package com.edu.dgut.controller;

import com.alibaba.fastjson.JSONObject;
import com.edu.dgut.dao.wUserRepository;
import com.edu.dgut.service.wUserService;
import com.edu.dgut.entity.wUserEntity;
import com.edu.dgut.service.mapObjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user/")
public class wUserController {

    @Autowired
    private wUserRepository wUser;

    @Autowired
    private wUserService wuserService;

    static final String appid = "wx3fc0e050853e45ff";

    static final String secret = "c154d9ec10a9de51e3d115e1e2132ca8";

    private mapObjectService mapObject = new mapObjectService();

    //查询所有微信用户
    @RequestMapping("getAllwUser")
    @ResponseBody
    public Map<String, Object> findAll(){
        List<wUserEntity> list = new ArrayList<>();
        list = wUser.findAll();
        return mapObject.put(list);
    }

    //通过open_id查找微信用户
    @RequestMapping("getByOpenId")
    @ResponseBody
    public Map<String,Object> getByOpenId(String open_id) {
        List<wUserEntity> wuser = wUser.findByOpenId(open_id);
        return mapObject.put(wuser);
    }

    //通过微信昵称查找微信用户
    @RequestMapping("getBywUserName")
    @ResponseBody
    public Map<String,Object> getBywUserName(String name) {
        List<wUserEntity> wuser = wUser.findByNameLike("%"+name+"%");
        return mapObject.put(wuser);
    }

    //添加或修改微信用户信息
    @RequestMapping("modifyWUser")
    @ResponseBody
    public Map<String, Object> saveOrModifyWUser(@RequestBody wUserEntity wuser) {
        //如果输入的open_id对应信息不存在，则插入新信息，否则修改open_id对应的各类信息
        List<wUserEntity> old_wuser = wUser.findByOpenId(wuser.getOpenId());
        if(old_wuser.size() != 0){
            wuser.setWuserId(old_wuser.get(0).getWuserId());
        }
        wuser = wUser.save(wuser);
        Map<String,Object> map = new HashMap<String,Object>();
        if(wuser != null) {
            map.put("status", 1);
        }else {
            map.put("status", 0);
        }
        return map;
    }

    //删除微信用户
    @RequestMapping("deleteByOpenId")
    @ResponseBody
    public Map<String, Object> deleteByOpenId(String open_id) {
        int i = wUser.deleteByOpenId(open_id);
        Map<String,Object> map = new HashMap<String,Object>();
        if(i >= 1) {
            map.put("status", 1);
        }else {
            map.put("status", 0);
        }
        return map;
    }

    //获取微信用户信息并返回
    @RequestMapping("getwUser")
    @ResponseBody
    public JSONObject authCode2Session(@RequestBody String jsCode) {
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + jsCode + "&grant_type=authorization_code";
        //发送请求
        String str = wuserService.sendGet(url);
        if (StringUtils.isEmpty(str)) {
            return null;
        } else {
            return JSONObject.parseObject(str);
        }
    }
}
